import { route, GET, POST } from 'awilix-express';

@route('/sums')
export default class SumsApi {

  constructor({ sumsService }) {
    this.sumsService = sumsService;
  }

  @route('/getSumsAll')
  @POST()
  async getSumsAll(req, res){
    const {
      mark
    } = req.body
    const { success, data: list, message } = await this.sumsService.getSumsAll({mark});
    if (!success) {
      res.failPrint(message);
      return;
    }
    res.successPrint(message, list);
  }

  @route('/getSumslist')
  @POST()
  async getSumslist(req, res) {
    const { t_name, mark, limit, offset } = req.body
    const { success, data: {count, rows}, message } = await this.sumsService.getSumslist({t_name, mark, limit, offset});
    if (!success) {
      res.failPrint(message);
      return;
    }
    res.successPrint(message, {count, rows});
  }

  @route('/addSums')
  @POST()
  async addSums(req, res) {
    const { 
      t_name,
      t_type,
      t_sumHttps,
      t_virtual,
      t_loadSearchFailed,
      t_loadSearchClosed,
      t_loadLogoFailed,
      t_invalidLogo,
      t_invalidIP,
      t_invalidSearch,
      t_success,
      startTime,
      updateTime,
      mark
     } = req.body;
    const { success, message } = await this.sumsService.addSums({
      t_name,
      t_type,
      t_sumHttps,
      t_virtual,
      t_loadSearchFailed,
      t_loadSearchClosed,
      t_loadLogoFailed,
      t_invalidLogo,
      t_invalidIP,
      t_invalidSearch,
      t_success,
      startTime,
      updateTime,
      mark
    });
    if (!success) {
      res.failPrint(message);
      return;
    }
    res.successPrint(message);
  }

  @route('/delSums')
  @POST()
  async delSums(req, res) {
    const { id, mark } = req.body;
    const { success, message } = await this.sumsService.delSums({id, mark});
    if (!success) {
      res.failPrint(message);
      return;
    }
    res.successPrint(message);
  }


  @route('/updateSums')
  @POST()
  async updateSums(req, res) {
    const {
      id,
      t_name,
      t_type,
      t_sumHttps,
      t_virtual,
      t_loadSearchFailed,
      t_loadSearchClosed,
      t_loadLogoFailed,
      t_invalidLogo,
      t_invalidIP,
      t_invalidSearch,
      t_success,
      startTime,
      updateTime,
    } = req.body;
    const { success, message } = await this.sumsService.updateSums({
      id,
      t_name,
      t_type,
      t_sumHttps,
      t_virtual,
      t_loadSearchFailed,
      t_loadSearchClosed,
      t_loadLogoFailed,
      t_invalidLogo,
      t_invalidIP,
      t_invalidSearch,
      t_success,
      startTime,
      updateTime,
    });
    if (!success) {
      res.failPrint(message);
      return;
    }
    res.successPrint(message);
  }
}